using System;
using UnityEngine;

public class EconomyTimer : MonoBehaviour
{
	public delegate void TimerFinishedEvent();

	private DateTime startDate;

	private float timerDuration;

	private int timeRemainingInSeconds;

	private string timeRemainingAsString;

	private string timeRemainingAsStringShort;

	private DateTime endDate;

	private bool running;

	public TimerFinishedEvent OnTimerFinished;

	public void Init(float newTime)
	{
		startDate = DateTime.UtcNow;
		timerDuration = newTime;
		endDate = startDate.AddSeconds(timerDuration + 1f);
	}

	public void ResetTimer(float newTime)
	{
		StopTimer();
		startDate = DateTime.UtcNow;
		timerDuration = newTime;
		endDate = startDate.AddSeconds(timerDuration + 1f);
	}

	public void StartTimer()
	{
		if (running)
		{
			Debug.Log("Timer running , call stop timer before starting new one!");
		}
		else
		{
			running = true;
		}
	}

	private void StopTimer()
	{
		running = false;
	}

	public string GetTimeRemaining()
	{
		return timeRemainingAsString;
	}

	public string GetTimeRemainingShortened()
	{
		return timeRemainingAsStringShort;
	}

	public int GetTimeRemainingInSeconds()
	{
		return timeRemainingInSeconds;
	}

	private void Update()
	{
		if (!running)
		{
			timeRemainingAsString = "0s";
			timeRemainingAsStringShort = "0m:00s";
			return;
		}
		DateTime utcNow = DateTime.UtcNow;
		int num = utcNow.CompareTo(endDate);
		if (num >= 0)
		{
			running = false;
			timeRemainingInSeconds = 0;
			timeRemainingAsString = "0s";
			timeRemainingAsStringShort = "0m:00s";
			if (OnTimerFinished != null)
			{
				OnTimerFinished();
			}
		}
		TimeSpan timeSpan = endDate - utcNow;
		try
		{
			timeRemainingInSeconds = (int)timeSpan.TotalSeconds;
			float num2 = (float)timeSpan.TotalMinutes;
			if (num2 < 0f)
			{
				timeRemainingAsString = "0s";
				timeRemainingAsStringShort = "0m:00s";
				return;
			}
		}
		catch (ArgumentOutOfRangeException ex)
		{
			Debug.Log(ex.Message);
			timeRemainingInSeconds = 0;
			timeRemainingAsString = "0s";
			timeRemainingAsStringShort = "0m:00s";
			return;
		}
		SetTimeRemainingString(timeSpan);
		SetTimeRemainingShortString(timeSpan);
	}

	private void SetTimeRemainingString(TimeSpan dateDiff)
	{
		try
		{
			if (dateDiff.Days != 0)
			{
				if (dateDiff.Days == 1)
				{
					timeRemainingAsString = dateDiff.Days + " day";
				}
				else
				{
					timeRemainingAsString = dateDiff.Days + " days";
				}
			}
			else if (dateDiff.Hours != 0)
			{
				timeRemainingAsString = dateDiff.Hours + "h:" + dateDiff.Minutes.ToString("D2") + "m:" + dateDiff.Seconds.ToString("D2") + "s";
			}
			else if (dateDiff.Minutes != 0)
			{
				timeRemainingAsString = dateDiff.Minutes + "m:" + dateDiff.Seconds.ToString("D2") + "s";
			}
			else
			{
				timeRemainingAsString = dateDiff.Seconds + "s";
			}
		}
		catch (ArgumentOutOfRangeException ex)
		{
			Debug.Log(ex.Message);
			timeRemainingInSeconds = 0;
			timeRemainingAsString = string.Empty;
		}
	}

	private void SetTimeRemainingShortString(TimeSpan dateDiff)
	{
		try
		{
			if (dateDiff.Days != 0)
			{
				timeRemainingAsStringShort = dateDiff.Days + "d:" + dateDiff.Hours + "h";
			}
			else if (dateDiff.Hours != 0)
			{
				timeRemainingAsStringShort = dateDiff.Hours + "h:" + dateDiff.Minutes + "m";
			}
			else
			{
				timeRemainingAsStringShort = dateDiff.Minutes + "m:" + dateDiff.Seconds.ToString("D2") + "s";
			}
		}
		catch (ArgumentOutOfRangeException ex)
		{
			Debug.Log(ex.Message);
			timeRemainingInSeconds = 0;
			timeRemainingAsStringShort = "0m:00s";
		}
	}

	public static EconomyTimer AddTimer(GameObject go, float duration)
	{
		if (go == null)
		{
			return null;
		}
		EconomyTimer economyTimer = go.AddComponent<EconomyTimer>();
		if (economyTimer != null)
		{
			economyTimer.ResetTimer(duration);
		}
		return economyTimer;
	}
}
